<?php

App::uses('FormHelper', 'View/Helper');

class MyFormHelper extends FormHelper {

	private $_fields = array();
	private $_rules = array();
	private $_messages = array();
	private $_id = null;
	
	public $accepted = array();
	public $helpers = array('Html', 'Js');
	
	public function __construct(View $View, $settings = array()) {
		parent::__construct($View, $settings);
		
		$this->accepted = array(
			'notempty' => array('rule' => 'required', 'format' => array('boolean', true)),
			'email' => array('rule' => 'email', 'format' => array('boolean', true)),
			'between' => array('rule' => 'rangelength', 'format' => array('array', range(1, 2))),
			'cc' => array('rule' => 'creditcard', 'format' => array('boolean', true)),
			'extension' => array('rule' => 'accept', 'format' => array('implode', 1, '|')),
			'maxLength' => array('rule' => 'maxlength', 'format' => array('value', 1)),
			'minLength' => array('rule' => 'minlength', 'format' => array('value', 1)),
			'numeric' => array('rule' => 'number', 'format' => array('boolean', true)),
			'range' => array('rule' => 'range', 'format' => array('array', range(1, 2))),
			'url' => array('rule' => 'url', 'format' => array('boolean', true)),
		);
	}
	
	protected function _initInputField($field, $options = array()) {
		$this->_fields[] = $field;
		return parent::_initInputField($field, $options);
	}
	
	public function create($model = null, $options = array()) {
		if(!empty($model) && !is_array($model)) {
			$this->defaultModel = $model;
		}
		
		if(!isset($options['id'])) {
			$domId = isset($options['action']) ? $options['action'] : $this->request['action'];
			$this->_id = $this->domId($model . '.' . $domId . '.Form');
		} else {
			$this->_id = $options['id'];
		}
		return parent::create($model, $options);
	}
	
	public function end($options = null) {
		if(!($options['validate'] === false)) {
			$this->_validate();
		}

		if(is_array($options) && !empty($options['validate'])) {
			unset($options['validate']);
		}
		return parent::end($options);
	}
	
	public function _validate() {
		foreach($this->_fields AS $field) {
			$model = $this->defaultModel;
			$nr = null;
			if(strstr($field, '.') !== false) {
				list($model, $field, $nr) = explode('.', $field, 3);
			}
			if(!empty($this->_models[$model]) && !empty($this->_models[$model]->validate[$field])) {
				$rules = $this->_models[$model]->validate[$field];
				$options = parent::_initInputField($field);
				if(is_array($rules)) {
					foreach($rules AS $rule) {
						$this->_addRule($options['name'], $rule);
					}
				}
			}	
		}
		$this->_View->element('validation', array(
			'rules' => $this->_rules,
			'messages' => $this->_messages,
			'id' => $this->_id
		));
	}
	
	private function _addRule($name, $rule) {		
		if(!is_array($rule) && array_key_exists($rule, $this->accepted)) {
			if($this->accepted[$rule]['format'][0] == 'boolean') {
				$this->_rules[$name][$this->accepted[$rule]['rule']] = $this->accepted[$rule]['format'][1];
			}
		} else if(!empty($rule['rule'][0]) && array_key_exists($rule['rule'][0], $this->accepted)) {
			$ruleName = $rule['rule'][0];
			if($this->accepted[$ruleName]['format'][0] == 'boolean') 
			{
				$this->_rules[$name][$this->accepted[$ruleName]['rule']] = $this->accepted[$ruleName]['format'][1];
			} 
			else if($this->accepted[$ruleName]['format'][0] == 'value') 
			{
				$this->_rules[$name][$this->accepted[$ruleName]['rule']] = $rule['rule'][$this->accepted[$ruleName]['format'][1]];
			} 
			else if($this->accepted[$ruleName]['format'][0] == 'array') 
			{
				foreach($this->accepted[$ruleName]['format'][1] AS $i) 
				{
					$this->_rules[$name][$this->accepted[$ruleName]['rule']][] = $rule['rule'][$i];
				}
			} 
			else if($this->accepted[$ruleName]['format'][0] == 'implode') 
			{
				$this->_rules[$name][$this->accepted[$ruleName]['rule']] = implode($rule['rule'][$this->accepted[$ruleName]['format'][1]], $this->accepted[$ruleName]['format'][2]);
			}
			
			if(!empty($rule['message'])) {
				$this->_messages[$name][$ruleName] = $rule['message'];
			}
			
			if(!empty($rule['required']) && $rule['required'] === true) {
				$this->_rules[$name]['required'] = true;
			}
		}
	}
}
